package com.example.music.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.event.ProgressEvent;
import com.aliyun.oss.event.ProgressEventType;
import com.aliyun.oss.event.ProgressListener;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.example.music.service.OssService;
import com.example.music.utils.ConstantPropertiesUtils;
import com.example.music.utils.PutObjectProgressListener;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.joda.time.DateTime;


import java.io.InputStream;
import java.util.UUID;

@Service
public class OssServiceImpl implements OssService {

    //上传音乐到oss
    @Override
    public String uploadFileMusic(MultipartFile file) {
        //工具类获取值
        String endpoint = ConstantPropertiesUtils.END_POIND;
        String accessKeyId=ConstantPropertiesUtils.ACCESS_KEY_ID;
        String accessKeySecret=ConstantPropertiesUtils.ACCESS_KEY_SECRET;
        String bucketName=ConstantPropertiesUtils.BUCKET_NAME;

        try {
            //创建oss实例
            OSS ossclient=new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);

            // 创建文件元信息。
            ObjectMetadata meta = new ObjectMetadata();
            meta.setHeader("MP3","audio/mpeg");
            meta.setContentDisposition("flac转mp3测试");
            // 设置内容被下载时的编码格式。
            meta.setContentEncoding("utf-8");
            //获取上传文件输入流
            InputStream inputStream=file.getInputStream();
            //
            System.out.println("Bytes "+file.getSize());


            //获取文件名称
            String fileName = file.getOriginalFilename();

            //获取文件名称
            String caselsh = fileName.substring(0,fileName.lastIndexOf("."));
//            System.out.println(caselsh);

            //获取文件类型
            String fileTyle=fileName.substring(fileName.lastIndexOf("."),fileName.length());
//            System.out.println(fileTyle);

            if (fileTyle.equals(".flac")){
                fileName=caselsh+".mp3";
            }
/*
            //1在文件名称里面添加随机唯一值
            String uuid= UUID.randomUUID().toString().replace("-","");
            filename=uuid+filename;

            //2吧文件安卓日期分类
            //获取当前日期
            String dataPath=new  DateTime().toString("yyyy/MM/dd");
            //拼接
            filename=dataPath+"/"+filename;
*/
            String music="music";
            fileName=music+"/"+fileName;
            //调用oss方法实现上传
            //第一个参数 Bucket名称
            //第二个参数 上传到oss文件路径和文件名称
            //第三个参数 上传文件流
          ossclient.putObject(bucketName,fileName,inputStream,meta);

            //进度条
//             ossclient.putObject(new PutObjectRequest(bucketName,fileName,inputStream).<PutObjectRequest>withProgressListener(new PutObjectProgressListener(file.getSize())));
            //关闭OssClient
            ossclient.shutdown();

            //把上传之后文件路径返回
            //需要把上传到阿里云oss路径手动拼接出来
            String url="https://"+bucketName+"."+endpoint+"/"+fileName;

            return  url;

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }






}
